package com.pbq.pickerlib.wheel;

import android.content.Context;
import android.view.ViewGroup;


import com.pbq.pickerlib.wheel.adapters.WheelTextAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by remilia on 2016/10/12.
 */
public class TextWheelManager {

    private Context mContext;
    private List<WheelTextAdapter> mList = new ArrayList<>();
    private List<WheelView> mWheelViews = new ArrayList<>();

    protected TextWheelManager(Context context, WheelTextAdapter... adapters) {
        mContext = context;
        for (WheelTextAdapter adapter : adapters) {
            mList.add(adapter);
            WheelView view = new WheelView(mContext);
            view.setViewAdapter(adapter);
            mWheelViews.add(view);
        }
        init();
    }

    private void init() {
        int length = mList.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                continue;
            }
            final int num = i;
            mWheelViews.get(i).addScrollingListener(new OnWheelScrollListener() {
                @Override
                public void onScrollingStarted(WheelView wheel) {

                }

                @Override
                public void onScrollingFinished(WheelView wheel) {
                    if (mOnScrollSelectListener != null) {
                        mOnScrollSelectListener.onScrollSelect(mContext, mWheelViews, num, mList);
                    }
                }
            });
        }
    }

    public int getCount() {
        return mList.size();
    }

    public List<WheelView> getWheelViews() {
        return getWheelViews(null, null);
    }

    public List<WheelView> getWheelViews(ViewGroup.LayoutParams params, ViewGroup parent) {
        if (params != null) {
            for (WheelView view : mWheelViews) {
                view.setLayoutParams(params);
                if (parent != null) {
                    parent.addView(view);
                }
            }
        }
        return mWheelViews;
    }

    private OnScrollSelectListener mOnScrollSelectListener;

    public void setOnScrollSelectListener(OnScrollSelectListener OnScrollSelectListener) {
        this.mOnScrollSelectListener = OnScrollSelectListener;
    }

    public interface OnScrollSelectListener {
        void onScrollSelect(Context context, List<WheelView> wheelViews, int positionP, List<WheelTextAdapter> adapters);
    }

    public ArrayList<String> getResult() {
        ArrayList<String> result = new ArrayList<>();
        int length = mList.size();
        for (int i = 0; i < length; i++) {
            result.add(String.valueOf(mList.get(i).getItemText(
                    mWheelViews.get(i).getCurrentItem())));
        }
        return result;
    }

    public void setCheckNum(int... checkNums) {
        for (int i = 0; i < checkNums.length; i++) {
            if (mList.get(i).getItemsCount() >= checkNums[i]) {
                mWheelViews.get(i).setCurrentItem(checkNums[i]);
            }
        }
    }

    public void setCheckResult(String... checkStrs) {
        for (int i = 0; i < checkStrs.length; i++) {
            List<String> strs = mList.get(i).getList();
            int index = strs.indexOf(checkStrs[i]);
            if (index != -1) {
                mWheelViews.get(i).setCurrentItem(index);
            }
        }
    }
}
